module.exports = {
    getPosterPath,
    getTorrentPath,
    getByKey,
    getTorrentId,
    getFileOrFolder
}

const path = require('path')
const config = require('../../config')
const StorageInstant = require('./../../lib/storage')
const Indexer = require('./../../lib/indexer')

// Expects a torrentSummary
// Returns an absolute path to the torrent file, or null if unavailable
function getTorrentPath(torrentSummary) {
    if (!torrentSummary || !torrentSummary.torrentFileName) return null
    return path.join(config.TORRENT_PATH, torrentSummary.torrentFileName)
}

// Expects a torrentSummary
// Returns an absolute path to the poster image, or null if unavailable
function getPosterPath(torrentSummary) {
    if (!torrentSummary || !torrentSummary.posterFileName) return null
    const posterPath = path.join(config.POSTER_PATH, torrentSummary.posterFileName)
    // Work around a Chrome bug (reproduced in vanilla Chrome, not just Electron):
    // Backslashes in URLS in CSS cause bizarre string encoding issues
    return posterPath.replace(/\\/g, '/')
}

// Expects a torrentSummary
// Returns a torrentID: filename, magnet URI, or infoHash
function getTorrentId(torrentSummary, tcb) {
    if (!torrentSummary) {
        return tcb('torrentSummary is null')
    }
    const s = torrentSummary
    var key = path.join(config.TORRENT_PATH, s.infoHash)
    var sTorrentFile = StorageInstant.get(key, true)
    if (sTorrentFile && sTorrentFile.length > 100) {
        // Load torrent from torrentFile buffer
        var torrentFile = new Buffer(sTorrentFile, 'base64')
        if (torrentFile) {
            return tcb(null, torrentFile)
        }
    }
    Indexer.metaGet(s.infoHash, (err, value) => {
        if (value && value.data && value.data.length > 100) {
            var torrentFile = new Buffer(value.data, 'base64')
            if (torrentFile) {
                return tcb(null, torrentFile)
            }
        }
        return tcb(null, s.magnetURI || s.infoHash)
        // if (s.torrentFileName) { // Load torrent file from disk
        //     return getTorrentPath(s)
        // } else { // Load torrent from DHT
        //     return s.magnetURI || s.infoHash
        // }
    })


}

// Expects a torrentKey or infoHash
// Returns the corresponding torrentSummary, or undefined
function getByKey(state, torrentKey) {
    if (!torrentKey) return undefined
    return state.saved.torrents.find((x) =>
        x.torrentKey === torrentKey || x.infoHash === torrentKey)
}

// Returns the path to either the file (in a single-file torrent) or the root
// folder (in  multi-file torrent)
// WARNING: assumes that multi-file torrents consist of a SINGLE folder.
// TODO: make this assumption explicit, enforce it in the `create-torrent`
// module. Store root folder explicitly to avoid hacky path processing below.
function getFileOrFolder(torrentSummary) {
    const ts = torrentSummary
    if (!ts.path || !ts.files || ts.files.length === 0) return null
    const dirname = ts.files[0].path.split(path.sep)[0]
    return path.join(ts.path, dirname)
}